// pages/user_end/user_info/user_info.js
const app = getApp();
const request = require('../../../utils/request.js');
const url = require('../../../utils/url.js');
const cookie = require('../../../utils/cookie.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    region: [],
    username: '',
    mobile: '',
    avatar: '',
    username: "",
    token: "",
    index: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    cookie.getCookie('userInfo', function (res) {
      if (res) {
        var etime = Date.parse(new Date()) / 1000;
        if (etime < res.expiretime) {
          that.setData({
            login: 1,
            token: res.token
          })
          that.getUserInfo();
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  getUserInfo: function () {
    var that = this;
    let param = {
      scoure: 'wxapp',
      token: that.data.token
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.getuserinfo, param, url.REQUEST_METHOD_GET).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
      if (res.data.code == 1) {
        that.setData({
          avatar: res.data.data.avatar,
          mobile: res.data.data.mobile,
          username: res.data.data.username
        })
      }
    })
  },
  changeAvatar: function () {
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var avatarSrc = res.tempFilePaths
        let avatar = res.tempFilePaths[0];
        console.log("FilePathsList:" + res);
        wx.uploadFile({
          url: url.HttpServerHostAddress + url.uploadpic,
          filePath: res.tempFilePaths[0],
          name: 'file',
          formData: {
            token: that.data.token,
          }, // HTTP 请求中其他额外的 form data
          success: function (res) {
            console.log(res)
            if (JSON.parse(res.data).code == 1) {
              console.log(JSON.parse(res.data).data);
              var url = JSON.parse(res.data).data.fullurl
              that.setData({
                avatar: url,
              })
            } else {
              wx.showToast({
                title: JSON.parse(res.data).msg,
                icon: 'none',
                duration: 3000
              })
              return;
            }
          }
        })
      }
    })
  },
  updateInfo: function () {
    var that = this;
    if (!that.data.username) {
      wx.showToast({
        title: '请输入昵称',
        icon: 'none'
      })
      return false;
    }
    let param = {
      scoure: 'wxapp',
      avatar: that.data.avatar,
      name: that.data.username,
      token: that.data.token
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.edituser, param, url.REQUEST_METHOD_POST).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
      if(res.data.code == 1) {
        var userInfo = wx.getStorageSync('userInfo');
        userInfo.username = that.data.username;
        userInfo.avatar = that.data.avatar;
        wx.setStorageSync('userInfo', userInfo);
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          success: function(){
            wx.navigateBack();
          }
        })
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
        })
      }

    })
  },
  changename: function (e) {
    var name = e.detail.value;
    this.setData({
      username: name
    })
  }
})